home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / virus / stelth22.zip / CALCCRC.C < prev    next >
C/C++ Source or Header  |  1992-02-10  |  2KB  |  59 lines

  1. /*
  2. calccrc.c
  3. Stealth Bomber Version 2.2
  4.  
  5. Kevin Dean
  6. Fairview Mall P.O. Box 55074
  7. 1800 Sheppard Avenue East
  8. Willowdale, Ontario
  9. CANADA    M2J 5B9
  10. CompuServe ID: 76336,3114
  11.  
  12. February 10, 1992
  13.  
  14.     This module calculates the CRC of a file.
  15.  
  16.     This code is public domain.
  17. */
  18.  
  19.  
  20. #include <stdio.h>
  21.  
  22. #include "vircheck.h"
  23.  
  24.  
  25. /* Macros to extract low and high bytes of a word. */
  26. #define lowb(x)  (*(unsigned char *)&(x))
  27. #define hib(x)   (*((unsigned char *)&(x) + 1))
  28.  
  29. /* Macros to extract low and high words of a dword. */
  30. #define loww(x)  (*(unsigned short *)&(x))
  31. #define hiw(x)   (*((unsigned short *)&(x) + 1))
  32.  
  33.  
  34. /***/
  35. /* Calculate the CRC of a file. The file is assumed to be open and the buffer is assumed to be valid. */
  36. crc32_t calccrc(FILE *f, byte *buffer, size_t bufsize, crc32_t polynomial)
  37. {
  38. crc32_t table[256];        /* CRC table. */
  39. register size_t i;        /* Byte counter. */
  40. register crc32_t *halfi;    /* Pointer to CRC of i / 2. */
  41. crc32_t crc;            /* Current CRC. */
  42. byte *bufptr;            /* Pointer to walk through buffer. */
  43.  
  44. /* Generate a CRC lookup table for faster calculation. */
  45. for (i = 0, halfi = table, table[0] = 0; i < 256; i += 2, halfi++)
  46.   if (hib(hiw(*halfi)) & 0x80)
  47.     table[i] = (table[i + 1] = *halfi << 1) ^ polynomial;
  48.   else
  49.     table[i + 1] = (table[i] = *halfi << 1) ^ polynomial;
  50.  
  51. /* Calculate CRC. */
  52. crc = 0;
  53. while ((i = fread(buffer, 1, bufsize, f)) != 0)
  54.   for (bufptr = buffer; i--; bufptr++)
  55.     crc = (crc << 8) ^ table[hib(hiw(crc)) ^ *bufptr];
  56.  
  57. return (crc);
  58. }
  59.